package com.shed.demo.dwr;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.shed.base.domain.Page;
import com.shed.base.utils.Md5PasswordEncoder;
import com.shed.demo.domain.Menu;
import com.shed.demo.domain.User;
import com.shed.demo.service.MenuService;
import com.shed.demo.service.UserService;

public class SmDwr extends BaseDwr {
	private static final long serialVersionUID = 6912861534323592006L;

	private MenuService menuService;
	private UserService userService;

	public void logoff() {
		DwrAuthHelper.removeUser();
	}

	public Map<String, Object> login(String userName, String userPwd) {
		Map<String, Object> result = userService.doLogin(userName, Md5PasswordEncoder.encodePassword(userPwd, null),
				getIpAddress());
		if (Boolean.valueOf(result.get("flag").toString())) {
			DwrAuthHelper.setUser(result.get("user"));
		}
		return result;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public void setMenuService(MenuService menuService) {
		this.menuService = menuService;
	}

	public User getCurrentUser() {
		try {
			return super.getCurrentUser();
		} catch (Exception e) {
			return new User();
		}
	}

	/**
	 * 根据上级编码取得所有子菜单
	 * 
	 * @param menuUpID
	 * @return
	 */
	public List<Menu> getMenu(Long menuUpID) {
		return menuService.getChildrenByUpID(menuUpID);
	}

	/**
	 * 添加菜单
	 * 
	 * @param menu
	 */
	public void createMenu(Menu menu) {
		menuService.createMenu(menu);
	}

	/**
	 * 修改菜单
	 * 
	 * @param menu
	 */
	public void updateMenu(Menu menu) {
		menuService.updateMenu(menu);
	}

	/**
	 * 删除菜单
	 * 
	 * @param menu
	 */
	public void deleteMenu(Long menuID) {
		menuService.deleteMenu(menuID);
	}

	/**
	 * 取菜单
	 * 
	 * @param menuID
	 */
	public Menu getMenuInfo(Long menuID) {
		return menuService.getMenuByMenuID(menuID);
	}

	/**
	 * 根据用户ID取用户
	 * 
	 * @param userId
	 * @return
	 */
	public User getUserByUserId(Long userId) {
		return userService.getUserByUserId(userId);
	}

	/**
	 * 根据用户名取用户
	 * 
	 * @param userName
	 * @return
	 */
	public User getUserByUserName(String userName) {
		return userService.getUserByUserName(userName);
	}

	/**
	 * 根据条件取用户列表
	 * 
	 * @param filter
	 * @return
	 */
	public List<User> findUser(Map<String, String> filter) {
		return userService.findUser(filter);
	}

	/**
	 * 根据条件取用户分页列表
	 * 
	 * @param filterMap
	 * @param likeMap
	 * @param sortMap
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	public Page pageUser(Map<String, String> filterMap, Map<String, String> likeMap, Map<String, String> sortMap,
			int pageNo, int pageSize) {
		return userService.pageUser(filterMap, likeMap, sortMap, pageNo, pageSize);
	}

	/**
	 * 创建用户
	 * 
	 * @param user
	 */
	public void createUser(User user) {
		userService.createUser(user);
	}

	/**
	 * 删除用户
	 * 
	 * @param userId
	 */
	public void deleteUser(Long userId) {
		userService.deleteUser(userId);
	}

	/**
	 * 修改用户
	 * 
	 * @param user
	 */
	public void updateUser(User user) {
		userService.updateUser(user);
	}

	/**
	 * 修改用户密码
	 * 
	 * @param userId
	 * @param userPwd
	 * @param preUserPwd
	 * @return
	 */
	public Map<String, String> updateUserPwd(Long userId, String userPwd, String preUserPwd) {
		Map<String, String> result = new HashMap<String, String>();
		User user = getUserByUserId(userId);
		if (user.getUserPwd().equals(Md5PasswordEncoder.encodePassword(preUserPwd, null))) {
			userService.updateUserPwd(userId, userPwd);
			result.put("flag", "true");
		} else {
			result.put("flag", "false");
			result.put("msg", "输入的当前密码不正确！");
		}

		return result;
	}
}
